import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import java.util.Properties;
import redis.clients.jedis.Jedis;

/**
 * @author : Ashiamd email: ashiamd@foxmail.com
 * @date : 2021/1/31 5:44 PM
 */
public class conka {

    public static JRedisUtil conn = new JRedisUtil("127.0.0.1",6379);
    public static void main(String[] args) throws Exception {
        // 创建执行环境

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 设置并行度1
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "192.168.174.141:9092,192.168.174.142:9092,192.168.174.143:9092");
        // 下面这些次要参数
        properties.setProperty("group.id", "consumer-group");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");

        // flink添加外部数据源
        DataStream<String> sourceStream = env.addSource(new FlinkKafkaConsumer<String>("example", new SimpleStringSchema(),properties));
        DataStream<String> filterStream = sourceStream.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                return value.startsWith("{\"data\":[{\"orderdetailid\"");
            }
        });
        DataStream<String> filterStream1 = sourceStream.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                return value.startsWith("{\"data\":[{\"order_id\"");
            }
        });
        DataStream<String> orderStream = filterStream
                .map(message->{
                    String jsonObj1 = JSON.parseObject(message).getString("data");
                    String a = jsonObj1.replace("]","");
                    String b = a.replace("[","");
                    JSONObject jsonObj = JSON.parseObject(b);
                    conn.incrBy("total",Float.parseFloat(jsonObj.getString("total").substring(0,jsonObj.getString("total").indexOf(".")+2)));
                    conn.incrBy("order_sum",1);
                    conn.sadd("rep",jsonObj.getString("recipient"));
                    conn.zincrby("place_rank",1,jsonObj.getString("receiveplace").substring(0,2));
                    conn.zincrby("zf_rank",1,jsonObj.getString("methodofpayment"));
                    return "数据成功输入到redis";
                });
        DataStream<String> orderStream1 = filterStream1
                .map(message1->{
                    String jsonObj = JSON.parseObject(message1).getString("data");
                    String a = jsonObj.replace("]","");
                    String b = a.replace("[","");
                    JSONObject jsonObj1 = JSON.parseObject(b);
                    conn.zincrby("book_rank",Integer.parseInt(jsonObj1.getString("book_num")),jsonObj1.getString("book_name"));
                    conn.zincrby("press_rank",Integer.parseInt(jsonObj1.getString("book_num")),jsonObj1.getString("book_press"));
                    return "数据成功输入到redis";
                });
        env.execute();
    }
}